﻿package mortal.game.scene3D
{
    import Message.Game.*;
    import Message.Public.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import baseEngine.system.*;
    import com.fyGame.fyMap.*;
    import com.gengine.core.*;
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.manager.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import extend.language.*;
    import extend.php.*;
    import flash.display.*;
    import flash.display3D.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.event.*;
    import frEngine.manager.*;
    import frEngine.render.layer.uiEffectLayer.*;
    import frEngine.util.*;
    import mortal.common.global.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.buff.*;
    import mortal.game.scene3D.display3d.blood.*;
    import mortal.game.scene3D.display3d.icon3d.*;
    import mortal.game.scene3D.display3d.text3d.dynamicText3d.*;
    import mortal.game.scene3D.display3d.text3d.staticText3d.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.fight.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.layer3D.model.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.scene3D.util.*;
    import mortal.game.view.hardware.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;

    public class GameScene3D extends Viewer3D
    {
        private var _addNum:int = 0;
        private var _disTime:int = 60;
        public var topMapLayer:MapLayer3D;
        public var bottomMapLayer:BottomMapLayer;
        public var playerLayer:PlayerLayer;
        public var dicePlayerLayer:EmbedPlayerLayerBase;
        private var _isOpenBlur:Boolean = false;
        public var mapExtendLayer:MapExtendLayer;
        public var gameCamera:GameCamera;
        public var rolePlayer:RolePlayer;
        private var _timer:FrameTimer;
        private var _isInitialize:Boolean = false;
        private var _isInLockScene:Boolean = false;
        private var _viewTarget:MovePlayer;
        private var _curSelectMesh:Mesh3D;
        private var _sceneEffect:EffectPlayer;
        private var _enterEffect:EffectPlayer;
        private var _flag:Boolean;
        protected var _isRemoveRole:Boolean = false;
        private var _mouseEnabled:Boolean = true;
        private var _sceneEffectValue:String;

        public function GameScene3D(param1:DisplayObjectContainer, param2:Number = 1, param3:Number = 0.5)
        {
            super(param1, param2, param3, false);
            Global3D.loadResource = true;
            this.version = ParamsConst.Flash3DVersion;
            this.initCamera();
            this.initParams();
            this.initLayers();
            this.mouseEnabled = false;
            Rect3DManager.instance.init(this);
            this.firstDrawFun = this.topMapLayer.drawImp;
            return;
        }// end function

        override public function setViewport(param1:Number = 0, param2:Number = 0, param3:Number = 640, param4:Number = 480, param5:int = 0) : void
        {
            super.setViewport(param1, param2, param3, param4, param5);
            Log.debug(param1, param2, param3, param4, param5);
            return;
        }// end function

        private function initCamera() : void
        {
            this.gameCamera = new GameCamera("Default_Scene_Camera");
            this.camera = this.gameCamera;
            this.gameCamera.updateProjectionMatrix();
            gameControler.shakeFun = this.upAndDownShake;
            return;
        }// end function

        public function upAndDownShake(param1:Boolean, param2:Number) : void
        {
            var _loc_5:Number = NaN;
            var _loc_3:Number = 0;
            var _loc_4:* = param2;
            if (param1)
            {
                _loc_5 = (this.rolePlayer.direction - 90) / 180 * Math.PI;
                _loc_3 = Math.sin(_loc_5) * param2;
                _loc_4 = Math.cos(_loc_5) * param2;
            }
            this.gameCamera.setShakeValue(_loc_3, 0, _loc_4);
            return;
        }// end function

        public function resetViewPort() : void
        {
            var _loc_1:* = SceneRange.display;
            this.setViewport(0, 0, _loc_1.width, _loc_1.height);
            Scene3DUtil.gameCameraCopy.updateProjectionMatrix();
            Scene3DUtil.UICamera.updateProjectionMatrix();
            Rect3DManager.instance.resize();
            return;
        }// end function

        private function initParams() : void
        {
            this.enableUpdateAndRender = false;
            this.checkEventPhase = true;
            this.backgroundColor = 3355443;
            this.autoResize = false;
            this.rolePlayer = RolePlayer.instance;
            return;
        }// end function

        private function initLayers() : void
        {
            this.bottomMapLayer = BottomMapLayer.instance;
            this.topMapLayer = new MapLayer3D(MapLayerType.MAPNORMAL, LoaderPriority.LevelD);
            this.mapExtendLayer = new MapExtendLayer("");
            this.playerLayer = new PlayerLayer("");
            this.addChild(this.bottomMapLayer);
            this.addChild(this.topMapLayer);
            this.addChild(this.mapExtendLayer);
            this.addChild(this.playerLayer);
            return;
        }// end function

        public function start() : void
        {
            if (this.context)
            {
                this.startEnterframe();
            }
            FrEventDispatcher.instance.proxyAddEventListener(this, Event.CONTEXT3D_CREATE, this.startEnterframe);
            return;
        }// end function

        override public function reUploadHander(param1:Boolean) : void
        {
            var _loc_2:Stage3D = null;
            if (this.context)
            {
                this.context.dispose();
                this.context = null;
                if (!param1)
                {
                    _loc_2 = stage.stage3Ds[this.stageIndex];
                    _loc_2.requestContext3D(Context3DRenderMode.AUTO);
                }
            }
            return;
        }// end function

        private function startEnterframe(event:Event = null) : void
        {
            FrameUtil.driveInfo = this.context.driverInfo;
            Program3dManager.instance.init();
            if (!this._timer)
            {
                this._timer = new FrameTimer(1, int.MAX_VALUE, true);
                this._timer.addListener(TimerType.ENTERFRAME, this.mainLoop);
            }
            this._timer.start();
            if (FrameUtil.driveInfo.indexOf("userDisabled") != -1)
            {
                HardwareTipWindow.instance.show((SceneRange.display.width - 400) / 2, (SceneRange.display.height - 300) / 2);
            }
            if (Global.hardwareState == 1)
            {
                MouseRightClickManager.instance.run();
            }
            if (Global3D.configNum == 1)
            {
                Blood3DResource.instance.init(this);
                Icon3DFactory.instance.init(this);
                Text3DFactory.instance.init(this);
                SText3DFactory.instance.init(this);
            }
            else
            {
                Blood3DResource.instance.reinit(this);
                Icon3DFactory.instance.reinit(this);
                Text3DFactory.instance.reinit(this);
                SText3DFactory.instance.reinit(this);
                this.bottomMapLayer.reUpload();
                this.topMapLayer.reUpload();
            }
            PHPSender.sendToURLByPHP(SenderType.FlashVersion, FlashVersion.instance.getVersionStr(), true);
            PHPSender.sendToURLByPHP(SenderType.PlayerDriver, FrameUtil.driveInfo + "||" + BrowerManager.instance.getBrowserType(), true);
            if (!FlashVersion.instance.isVersionMeet(11, 4))
            {
                Alert(Language.getString(20681));
            }
            return;
        }// end function

        private function onActivete(event:Event) : void
        {
            this.drawStage();
            return;
        }// end function

        private function addTestChat() : void
        {
            var _loc_1:* = new SChatMsg();
            _loc_1.chatType = EChatType._EChatTypeWorld;
            _loc_1.content = "/b4 /b3 /b2哈哈哈测试测试";
            _loc_1.font = 1;
            _loc_1.toEntityId = new SEntityId();
            _loc_1.chatDt = new Date();
            var _loc_2:* = new SMiniPlayer();
            _loc_2.entityId = new SEntityId();
            _loc_2.name = "张三";
            _loc_1.fromPlayer = _loc_2;
            NetDispatcher.dispatchCmd(ServerCommand.ChatMessageUpdate, _loc_1);
            return;
        }// end function

        private function mainLoop(param1:FrameTimer = null) : void
        {
            var _loc_2:Point = null;
            if (this.context == null)
            {
                return;
            }
            if (!Global.isMinimize)
            {
                if (param1.currentCount % (Global.stage.frameRate > 40 ? (6) : (3)) == 0 && this.isInitialize)
                {
                    _loc_2 = Scene3DUtil.getSceneMousePostion(stage.mouseX, stage.mouseY, false);
                    this.updateMouseOver(_loc_2.x, _loc_2.y);
                }
            }
            if (!param1.isRepair || Global.isMinimize)
            {
                MoveUtil.updateEntityPos();
                this._flag = !this._flag;
                if (this._flag && Global3D.prudenceFrame)
                {
                    return;
                }
                if (!param1.isRepair)
                {
                    if (this.viewTarget)
                    {
                        this.viewTarget.setViewCenter();
                    }
                    EffectPlayer.checkToParser();
                    this.update();
                    Global3D.drawCalls2d = 0;
                    this.drawStage();
                    GameStatistical.draw3dNum = Global3D.drawCalls3d;
                    GameStatistical.drawTopMapNum = Global3D.drawCalls2d;
                }
            }
            return;
        }// end function

        private function drawStage() : void
        {
            var curFps:int;
            var hasThread:Boolean;
            if (!this.paused)
            {
                curFps = FPS.instance.fpsNum;
                if (curFps > PerformanceUtil.instance.upgradeValue)
                {
                    var _loc_2:String = this;
                    var _loc_3:* = this._addNum + 1;
                    _loc_2._addNum = _loc_3;
                    if (this._addNum < 0)
                    {
                        this._addNum = 0;
                    }
                    if (this._addNum >= this._disTime && this._antialias != 2)
                    {
                        this.antialias = 2;
                        PerformanceUtil.instance.upgrade(curFps);
                    }
                    if (this._addNum > this._disTime + 300)
                    {
                        this._disTime = 60;
                        this._addNum = 0;
                        PerformanceUtil.instance.upgrade(curFps);
                    }
                }
                else
                {
                    if (this._antialias != 0)
                    {
                        this.antialias = 0;
                        PerformanceUtil.instance.downgrade(curFps);
                        if (this._addNum < this._disTime + 180)
                        {
                            this._disTime = this._disTime + 180;
                        }
                        this._addNum = 0;
                    }
                    var _loc_2:String = this;
                    var _loc_3:* = this._addNum - 1;
                    _loc_2._addNum = _loc_3;
                    if (this._addNum < -300)
                    {
                        this._addNum = 0;
                        PerformanceUtil.instance.downgrade(curFps);
                    }
                }
                hasThread = Global.threadManager != null;
                GameStatistical.drawType = String(this.antialias) + ":" + (hasThread ? (1) : (0)) + ":" + PerformanceUtil.instance.flag;
                try
                {
                    this.render(this.gameCamera, false);
                }
                catch (e:Error)
                {
                    Log.error("GameScene3D render error :" + e.message);
                    return;
                }
                if (this.writeToTexture)
                {
                    if (this.gameCamera.viewPort != this.viewPort)
                    {
                        this.gameCamera.viewPort = this.viewPort;
                        this.gameCamera.clipRectangle = null;
                        this.setupFrame(this.gameCamera);
                        Device3D.setViewPortRect(viewPort.width, viewPort.height, this.context);
                    }
                }
                if (!this.context)
                {
                    return;
                }
                this.context.setStencilActions(Context3DTriangleFace.FRONT_AND_BACK, Context3DCompareMode.ALWAYS, Context3DStencilAction.KEEP);
                this.context.clear(0, 0, 0, 0, 1, 1, Context3DClearMask.DEPTH);
                LastRender.instance.render(this.context);
                this.setupFrame(Scene3DUtil.gameCameraCopy);
                this.context.clear(0, 0, 0, 0, 1, 0, Context3DClearMask.DEPTH | Context3DClearMask.STENCIL);
                UIEffectList.instance.render();
                this.setupFrame(Scene3DUtil.UICamera);
                Rect3DManager.instance.renderRect3d();
                this.setupFrame(this.gameCamera);
                this.endFrame();
                this.context.setStencilActions(Context3DTriangleFace.FRONT_AND_BACK, Context3DCompareMode.ALWAYS, Context3DStencilAction.KEEP);
                this.context.setStencilReferenceValue(0);
                context.present();
            }
            return;
        }// end function

        public function updateMouseOver(param1:Number, param2:Number) : void
        {
            if (this._mouseEnabled)
            {
                ThingUtil.onMouseOver(param1, param2);
            }
            return;
        }// end function

        public function get isInitialize() : Boolean
        {
            return this._isInitialize;
        }// end function

        public function setPlayerPoint(param1:int, param2:int, param3:int) : void
        {
            this._isInitialize = false;
            var _loc_4:* = new SPoint();
            _loc_4.x = param2;
            _loc_4.y = param3;
            Cache.instance.role.entityInfo.points = [_loc_4];
            RolePlayer.instance.stopMove();
            this.loadMapData(param1);
            this.changeSceneEffect(MapFileUtil.mapInfo.sceneEffect);
            this.playEnterEffect(MapFileUtil.mapInfo.enterEffect);
            return;
        }// end function

        private function loadMapData(param1:int) : void
        {
            MapFileUtil.mapID = param1;
            MapLoader.instance.addEventListener(Event.COMPLETE, this.onLoadedHandler);
            MapLoader.instance.load();
            return;
        }// end function

        private function onLoadedHandler(event:Event) : void
        {
            this.initMapData(Game.mapInfo);
            this.initPlayer();
            this._isInitialize = true;
            FrEventDispatcher.instance.proxyDispatchEvent(this, SceneEvent.INIT);
            return;
        }// end function

        private function initMapData(param1:FyMapInfo) : void
        {
            Game.mapInfo = param1;
            var _loc_2:* = GameMapConfig.instance.getMapInfo(param1.mapId).bgInfo;
            if (_loc_2)
            {
                param1.bgMapWidth = _loc_2.mapWidth;
                param1.bgMapHeight = _loc_2.mapHeight;
            }
            GameMapUtil.init(param1);
            SceneRange.init(param1);
            MapConst.init(param1);
            AstarAnyDirection.copyMapData(param1.mapData);
            AstarAnyDirection.resetLink(true);
            this.bottomMapLayer.resetBgMapInfo(_loc_2.mapName, _loc_2.mapWidth, _loc_2.mapHeight, _loc_2.version, _loc_2.autoMove);
            return;
        }// end function

        public function initPlayer() : void
        {
            var _loc_1:Boolean = false;
            if (this._isRemoveRole)
            {
                return;
            }
            var _loc_2:* = Cache.instance.role.roleEntityInfo;
            if (this.playerLayer.contains(RolePlayer.instance) == false)
            {
                RolePlayer.instance.updateInfo(_loc_2, true);
                RolePlayer.instance.addToStage(this.playerLayer);
            }
            else
            {
                RolePlayer.instance.updateInfo(_loc_2, false);
            }
            RolePlayer.instance.walkBreak();
            ThingUtil.entityUtil.addPlayers(RolePlayer.instance.entityInfo.entityInfo.entityId, RolePlayer.instance);
            ThingUtil.isMoveChange = true;
            return;
        }// end function

        public function removeRole() : void
        {
            this._isRemoveRole = true;
            RolePlayer.instance.removeFromStage(this.playerLayer, true);
            return;
        }// end function

        public function addRole() : void
        {
            this._isRemoveRole = false;
            RolePlayer.instance.addToStage(this.playerLayer);
            return;
        }// end function

        public function get viewTarget() : MovePlayer
        {
            return this._viewTarget;
        }// end function

        public function set viewTarget(param1:MovePlayer) : void
        {
            this._viewTarget = param1;
            if (param1)
            {
                this._isInLockScene = true;
            }
            else
            {
                this._isInLockScene = false;
                RolePlayer.instance.setCurrentPointImpl();
            }
            return;
        }// end function

        public function set scrollRect(param1:Rectangle) : void
        {
            SceneRange.display = param1;
            this.topMapLayer.loadMapPiece(SceneRange.loadMapRange, SceneRange.drawMapRange);
            LayerManager.entityTalkLayer.updateTalkPosition();
            ThingUtil.shareCollectEntityUtil.updateCollectProBarXY();
            this.gameCamera.setScreenPos(SceneRange.displayInt);
            return;
        }// end function

        public function moveRoleByPath(param1:Array) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.RoleStartMoveByPath, param1));
            if (param1)
            {
                if (param1.length == 0)
                {
                    return;
                }
                if (GameMapUtil.curMapState.isPetBreakMap)
                {
                    ThingUtil.entityUtil.getSelfPetEntity().walking(param1);
                }
                else
                {
                    this.rolePlayer.inServerMove = false;
                    this.rolePlayer.walking(param1);
                }
            }
            return;
        }// end function

        public function addPointMark(param1:int, param2:int) : void
        {
            var _loc_3:* = SceneGlobalPlayer.getPointMask();
            _loc_3.x2d = param1;
            _loc_3.y2d = param2;
            _loc_3.play(PlayMode.ANIMATION_STOP_MODE);
            return;
        }// end function

        public function lockSceen() : void
        {
            this._isInLockScene = true;
            return;
        }// end function

        public function unLockSceen() : void
        {
            this._isInLockScene = false;
            return;
        }// end function

        public function tweenScrollRect(param1:Number, param2:Number, param3:Number = 0.5, param4:Function = null) : void
        {
            var rec:Rectangle;
            var getX:Function;
            var getY:Function;
            var toCenterX:* = param1;
            var toCenterY:* = param2;
            var time:* = param3;
            var onCompl:* = param4;
            getX = function () : Number
            {
                var _loc_1:* = toCenterX - Global.stage.stageWidth / 2;
                if (_loc_1 + Global.stage.stageWidth > SceneRange.map.right)
                {
                    _loc_1 = SceneRange.map.right - Global.stage.stageWidth;
                }
                _loc_1 = _loc_1 < 0 ? (0) : (_loc_1);
                return _loc_1;
            }// end function
            ;
            getY = function () : Number
            {
                var _loc_1:* = toCenterY - Global.stage.stageHeight / 2;
                if (_loc_1 + Global.stage.stageHeight > SceneRange.map.bottom)
                {
                    _loc_1 = SceneRange.map.bottom - Global.stage.stageHeight;
                }
                _loc_1 = _loc_1 < 0 ? (0) : (_loc_1);
                return _loc_1;
            }// end function
            ;
            this._isInLockScene = true;
            Global3D.stageFrame = 60;
            rec = new Rectangle(SceneRange.display.x, SceneRange.display.y, SceneRange.display.width, SceneRange.display.height);
            TweenMax.to(rec, time, {dynamicProps:{x:getX, y:getY}, onUpdate:function () : void
            {
                rec.width = Global.stage.stageWidth;
                rec.height = Global.stage.stageHeight;
                scrollRect = rec;
                return;
            }// end function
            , onComplete:function () : void
            {
                ThingUtil.isEntitySort = true;
                ThingUtil.isMoveChange = true;
                if (onCompl != null)
                {
                    onCompl.call();
                }
                return;
            }// end function
            });
            return;
        }// end function

        public function stopTweenScrollRect(param1:Number = 1, param2:Function = null) : void
        {
            var compl:Function;
            var time:* = param1;
            var onCompl:* = param2;
            compl = function () : void
            {
                _isInLockScene = false;
                RolePlayer.instance.setPixlePoint(RolePlayer.instance.x2d, RolePlayer.instance.y2d, false, false);
                if (onCompl != null)
                {
                    onCompl.call();
                }
                return;
            }// end function
            ;
            this.tweenScrollRect(RolePlayer.instance.x2d, RolePlayer.instance.y2d, time, compl);
            return;
        }// end function

        public function get isInLockScene() : Boolean
        {
            return this._isInLockScene;
        }// end function

        public function setMouseEnabled(param1:Boolean) : void
        {
            this._mouseEnabled = param1;
            return;
        }// end function

        public function getMouseEnabled() : Boolean
        {
            return this._mouseEnabled;
        }// end function

        override public function set sceneScale(param1:Number) : void
        {
            super.sceneScale = param1;
            SceneRange.sceneScale = param1;
            return;
        }// end function

        public function changeSceneEffect(param1:String) : void
        {
            var _loc_2:RenderList = null;
            if (param1)
            {
                if (this._sceneEffectValue != param1)
                {
                    this._sceneEffectValue = param1;
                    if (this._sceneEffect)
                    {
                        this._sceneEffect.dispose();
                    }
                    _loc_2 = UIEffectList.instance.getRenderListByUILayerID(UILayerIDs.ScreenID);
                    this._sceneEffect = EffectPlayerPool.instance.getEffectPlayer(param1, Game.scene.playerLayer, _loc_2, true);
                    this._sceneEffect.play(PlayMode.ANIMATION_LOOP_MODE);
                }
            }
            else if (this._sceneEffect)
            {
                this._sceneEffect.dispose();
                this._sceneEffect = null;
                this._sceneEffectValue = null;
            }
            return;
        }// end function

        public function playEnterEffect(param1:String) : void
        {
            if (this._enterEffect)
            {
                this._enterEffect.dispose();
                this._enterEffect = null;
            }
            if (param1)
            {
                if (!this._enterEffect)
                {
                    this._enterEffect = EffectPlayerPool.instance.getEffectPlayer(param1, Game.scene.playerLayer, UIEffectList.instance.getRenderListByUILayerID(UILayerIDs.TextID), false);
                    this._enterEffect.clearEnabled = false;
                    this._enterEffect.play(PlayMode.ANIMATION_STOP_MODE);
                    this._enterEffect.x = -250;
                    this._enterEffect.y = 50;
                }
            }
            else if (this._enterEffect)
            {
                this._enterEffect.dispose();
                this._enterEffect = null;
            }
            return;
        }// end function

        public function clearAll() : void
        {
            this._isInitialize = false;
            Cache.instance.role.roleEntityInfo.setNotFighting();
            ThingUtil.removeAll();
            this.topMapLayer.clearMap();
            Cache.instance.entity.clearAll();
            Cache.instance.passCrossCache.clearAll();
            LayerManager.entityTalkLayer.clear();
            SkillEffectUtil.removeAllChildren();
            BuffChainUtil.clearAll();
            FightChainUtil.clearAll();
            ResByteManager.dispose(true);
            EffectPlayerPool.instance.disposeAll();
            FightData.clear();
            return;
        }// end function

    }
}
